package com.cms.common.model.services.impl;


import com.cms.common.model.criteria.Criteria;
import com.cms.common.model.entity.Roles;
import com.cms.common.model.repository.RolesRepository;
import com.cms.common.model.services.RolesService;
import com.cms.common.model.vo.RequestData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("RolesServiceService")
public class RolesServiceServiceImpl implements RolesService {

    @Autowired
    RolesRepository rolesRepository;




    @Override
    public Page<Roles> getRolesByPage(RequestData requestData) {
        Criteria<Roles> criteria = new Criteria<>();
        requestData.setObjParams(criteria);
        Sort sort = Sort.by(Sort.Order.asc("updateTime"), Sort.Order.asc("id"));
        Pageable pageable = PageRequest.of(requestData.getPage() - 1, requestData.getPageSize(), sort);
        Page<Roles> roles = rolesRepository.findAll(criteria, pageable);
        return roles;
    }

    @Override
    public Roles getRolesId(Long id) {
        return rolesRepository.findById(id).orElse(null);
    }

    @Override
    public Roles addRoles(Roles roles) {
        return rolesRepository.saveAndFlush(roles);
    }

    @Override
    public void deleteRoles(Roles roles) {
        rolesRepository.delete(roles);
    }

    @Override
    public List<Roles> getAllRoles() {
        return rolesRepository.findAll();
    }
}
